<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//登录注册控制器
class LoginController extends Controller{
	public function index(){
		if(IS_POST){
			/**
			 * 验证用户名是否存在
			 */
			$username=Q('post.username');
			$data=Db::table('user')->where("username='{$username}'")->get();
//			p($data);die;
			if(!$data) View::error('用户名不正确');
			/**
			 * 验证密码是否匹配
			 */
			$upassword=Q('post.upassword','','md5');
			if($upassword!=$data[0]['upassword']){
				View::error('密码不正确');
			}
			/**
			 * 登录成功
			 */
			$_SESSION['username']=$username;
			$_SESSION['uid']=$data[0]['uid'];
			$_SESSION['upassword']=$upassword;
//			p($_SESSION);
			View::success('登录成功',U('Index/index'));
			
		}
			

		View::make();
	}

	public function zhuce(){
		if(IS_AJAX){
			$userModel=new \Common\Model\User;
			if(!$userModel->store()){
				$this->ajax(array('status'=>false,'message'=>$userModel->getError()));
			}else{
				$this->ajax(array('status'=>true,'message'=>'注册成功'));
			}
		}
			View::make();
	}	
	public function code(){
		Code::make();
	}
    public function out(){
        session_unset();
        session_destroy();
        //跳转到登陆页面即本页面的index方法
         go(U('Index/index'));
    }
	
}